package cn.edu.ujn.project.supermarket.controller;

import java.util.ArrayList;
import java.util.List;

import cn.edu.ujn.project.supermarket.extendbean.InportGoodsChild;
import cn.edu.ujn.project.supermarket.service.IGoodsInforService;
import cn.edu.ujn.project.supermarket.service.ISupplierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.project.common.annotation.Log;
import cn.edu.ujn.project.common.core.controller.BaseController;
import cn.edu.ujn.project.common.core.domain.AjaxResult;
import cn.edu.ujn.project.common.core.page.TableDataInfo;
import cn.edu.ujn.project.common.enums.BusinessType;
import cn.edu.ujn.project.common.utils.poi.ExcelUtil;
import cn.edu.ujn.project.supermarket.domain.InportGoods;
import cn.edu.ujn.project.supermarket.service.IInportGoodsService;

/**
 * 进货Controller
 * 
 * @author zhaomh
 * @date 2023-11-14
 */
@Controller
@RequestMapping("/system/inport")
public class InportGoodsController extends BaseController
{
    private String prefix = "system/inport";

    @Autowired
    private IInportGoodsService inportGoodsService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IGoodsInforService goodsInforService;

    @RequiresPermissions("system:inport:view")
    @GetMapping()
    public String inport()
    {
        return prefix + "/inport";
    }

    /**
     * 查询进货列表
     */
    @RequiresPermissions("system:inport:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InportGoodsChild inportGoods)
    {
        startPage();
//        List<InportGoods> list = inportGoodsService.selectInportGoodsList(inportGoods);
//        List<InportGoodsChild> listChild = new ArrayList<>();
//        for (InportGoods item : list){
//            listChild.add(new InportGoodsChild(this.supplierService.selectSupplierBySupplierId(item.getSupplierCode()).getName(), this.goodsInforService.selectGoodsInforByBarCode(item.getBarCode()).getName(), item));
//        }
        return getDataTable(this.inportGoodsService.selectInportGoodsChildList(inportGoods));
//        return getDataTable(listChild);
    }

    /**
     * 导出进货列表
     */
    @RequiresPermissions("system:inport:export")
    @Log(title = "进货", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InportGoods inportGoods)
    {
        List<InportGoods> list = inportGoodsService.selectInportGoodsList(inportGoods);
        ExcelUtil<InportGoods> util = new ExcelUtil<InportGoods>(InportGoods.class);
        return util.exportExcel(list, "进货数据");
    }

    /**
     * 新增进货
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存进货
     */
    @RequiresPermissions("system:inport:add")
    @Log(title = "进货", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InportGoods inportGoods)
    {
        return toAjax(inportGoodsService.insertInportGoods(inportGoods));
    }

    /**
     * 修改进货
     */
    @RequiresPermissions("system:inport:edit")
    @GetMapping("/edit/{inportId}")
    public String edit(@PathVariable("inportId") String inportId, ModelMap mmap)
    {
        InportGoods inportGoods = inportGoodsService.selectInportGoodsByInportId(inportId);
        mmap.put("inportGoods", inportGoods);
        return prefix + "/edit";
    }

    /**
     * 修改保存进货
     */
    @RequiresPermissions("system:inport:edit")
    @Log(title = "进货", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InportGoods inportGoods)
    {
        return toAjax(inportGoodsService.updateInportGoods(inportGoods));
    }

    /**
     * 删除进货
     */
    @RequiresPermissions("system:inport:remove")
    @Log(title = "进货", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(inportGoodsService.deleteInportGoodsByInportIds(ids));
    }
}
